using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using minega.Properties;

namespace minega
{
	public partial class frmMain : Form
	{
		Grid _grid;

		public frmMain()
		{
			InitializeComponent();
			_grid = new Grid(1000, 1000, 200000);
			statusToolStripLabel.Text = _grid.Mines.ToString() + " mines, " + _grid.Width + "x" + _grid.Height + ".";
            ConfigureScrollbars();
            CalcGridSize();
		}

		private void pictureBoxGame_Paint(object sender, PaintEventArgs e)
		{
			e.Graphics.DrawImage(_grid.Buffer, new Point(0, 0));
		}

		private void newGameToolStripButton_Click(object sender, EventArgs e)
		{
			_grid.NewGame();
			pictureBoxGame.Invalidate();
		}

		private void gridSizeToolStripButton_Click(object sender, EventArgs e)
		{
			frmGridSize gs = new frmGridSize(ref _grid);
			gs.ShowDialog(this);
			statusToolStripLabel.Text = _grid.Mines.ToString() + " mines, " + _grid.Width + "x" + _grid.Height + ".";
			ConfigureScrollbars();
			CalcGridSize();
			pictureBoxGame.Invalidate();
		}

		private void saveGameToolStripButton_Click(object sender, EventArgs e)
		{
			// Save Game
		}

		private void loadGameToolStripButton_Click(object sender, EventArgs e)
		{
			// Load Game
		}

		private void pictureBoxGame_Resize(object sender, EventArgs e)
		{
			// TODO: Rewrite this when the time comes
			// Maybe not so neat, but still good enough while testing
            CalcGridSize();
            pictureBoxGame.Invalidate();
            ConfigureScrollbars();
		}

        private void CalcGridSize()
        {
            int nCellsX = pictureBoxGame.Width / 16;
            int nCellsY = pictureBoxGame.Height / 16;
            _grid.Resize(nCellsX, nCellsY);
        }

        private void ConfigureScrollbars()
        {
            hScrollBar.Maximum = _grid.Width - _grid.VisibleWidth;
            hScrollBar.Value = _grid.OffsetX;

            vScrollBar.Maximum = _grid.Height - _grid.VisibleHeight;
            vScrollBar.Value = _grid.OffsetY;
        }

        private void vhScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            _grid.Move(hScrollBar.Value, vScrollBar.Value);
            pictureBoxGame.Invalidate();
		}

		private void pictureBoxGame_MouseDown(object sender, MouseEventArgs e)
		{
			int cellX = e.X / 16;
			int cellY = e.Y / 16;
			try
			{
				if (e.Button == MouseButtons.Left && !KeyboardInfo.GetKeyState(Keys.RButton).IsPressed)
				{
					if (KeyboardInfo.GetKeyState(Keys.ControlKey).IsPressed)
					{
						_grid.ToggleFlag(cellX, cellY);
						pictureBoxGame.Invalidate();
					}
					else
					{
						_grid.Press(cellX, cellY);
						pictureBoxGame.Invalidate();
					}
				}
				else if (e.Button == MouseButtons.Left && KeyboardInfo.GetKeyState(Keys.RButton).IsPressed)
				{
					_grid.PressNeighbours(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
				else if (e.Button == MouseButtons.Right && !KeyboardInfo.GetKeyState(Keys.LButton).IsPressed)
				{
					_grid.ToggleFlag(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
			}
			catch (IndexOutOfRangeException)
			{
			}
		}

		private void pictureBoxGame_MouseMove(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left && !KeyboardInfo.GetKeyState(Keys.RButton).IsPressed)
			{
				int cellX = e.X / 16;
				int cellY = e.Y / 16;
				try
				{
					_grid.Press(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
				catch (IndexOutOfRangeException)
				{
				}
			}
			else if (KeyboardInfo.GetKeyState(Keys.LButton).IsPressed && KeyboardInfo.GetKeyState(Keys.RButton).IsPressed)
			{
				int cellX = e.X / 16;
				int cellY = e.Y / 16;
				try
				{
					_grid.PressNeighbours(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
				catch (IndexOutOfRangeException)
				{
				}
			}
		}

		private void pictureBoxGame_MouseLeave(object sender, EventArgs e)
		{
			// TODO: Fix this, doesn't seem to get here...
			_grid.UnPress();
		}

        private void pictureBoxGame_MouseUp(object sender, MouseEventArgs e)
        {
            int cellX = e.X / 16;
            int cellY = e.Y / 16;
            try
			{
				if (e.Button == MouseButtons.Left && !KeyboardInfo.GetKeyState(Keys.RButton).IsPressed)
				{
					_grid.UnPress();
					_grid.Click(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
				else if (e.Button == MouseButtons.Right && KeyboardInfo.GetKeyState(Keys.LButton).IsPressed)
				{
					_grid.UnPress();
					_grid.ClickNeighbours(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
				else if (e.Button == MouseButtons.Left && KeyboardInfo.GetKeyState(Keys.RButton).IsPressed)
				{
					_grid.UnPress();
					_grid.ClickNeighbours(cellX, cellY);
					pictureBoxGame.Invalidate();
				}
            }
            catch (IndexOutOfRangeException)
            {
            }
        }

        private void frmMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Down)
                hScrollBar.Value = Math.Min(hScrollBar.Value + 1, hScrollBar.Maximum);
            else if (e.KeyChar == (char)Keys.Up)
                hScrollBar.Value = Math.Max(hScrollBar.Value - 1, hScrollBar.Minimum);
            else if (e.KeyChar == (char)Keys.Left)
                vScrollBar.Value = Math.Max(vScrollBar.Value - 1, vScrollBar.Minimum);
            else if (e.KeyChar == (char)Keys.Right)
                vScrollBar.Value = Math.Min(vScrollBar.Value + 1, vScrollBar.Maximum);

            vhScrollBar_Scroll(null, null);
        }
	}
}